#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from pyrogram import Client, filters
from pyrogram.raw import types, functions
from pyrogram.enums import ParseMode, MessageEntityType


api_id = 
api_hash = ""

app = Client("my_bot", api_id=api_id, api_hash=api_hash)

# message(sticker) | sticker set short name
def get_owner_id(data):
    try:
        short_name = data.reply_to_message.sticker.set_name
    except:  # noqa: E722
        if not isinstance(data, str):
            return None
        short_name = data
    sticker_set = app.invoke(
        functions.messages.GetStickerSet(
            stickerset=types.InputStickerSetShortName(short_name=short_name),
            hash=0
        )
    )
    owner_id = sticker_set.set.id >> 32
    return owner_id


def get_inline_bot_data(id):
    data = {"id": id, "name": None, "username": None}
    
    inline_search = app.get_inline_bot_results("tgdb_bot", str(id))
    
    if "\n" not in inline_search.results[0].send_message.message:
        return None
    
    temp = inline_search.results[0].send_message.message.split("\n\n")[0]
    temp = temp.split("\n")[2:]
    
    def split(x): return x.split(": ", 1)[1]
    
    data["name"] = split(temp[0])
    data["username"] = split(temp[1]) if len(temp) > 1 else None
    
    return data


def get_pyrogram_user_data(id):
    data = {"id": id, "name": None, "username": None}
    try:
        res = app.get_users(id)
        last = res.last_name if res.get('last_name') else ''
        data["name"] = f"{res.first_name} {last}"
        data["username"] = f"@{res.username}" if res.get("username") else None
        return data
    except:  # noqa: E722
        return None


def get_user_data(id):
    data = {"id": id, "name": None, "username": None}
    if data := get_inline_bot_data(id):
        return data
    if data := get_pyrogram_user_data(id):    
        return data
    return data


def get_emoji_set_names(message):

    custom_emojis = []
    for entity in message.reply_to_message.caption_entities:
        if entity.type == MessageEntityType.CUSTOM_EMOJI:
            custom_emojis.append(entity.custom_emoji_id)
    custom_emojis = app.get_custom_emoji_stickers(set(custom_emojis))
    set_names = [emoji.set_name for emoji in custom_emojis]
    return tuple(set(set_names))


def get_user_data_text(message):
    if owner_id := get_owner_id(message):
        data = get_user_data(owner_id)
        text = f"ID: `{owner_id}`\n"
        if name := data["name"]:
            text += f"Name: `{name}`\n"
        if username := data["username"]:
            text += f"Username: {username}\n"
        return text
    return ""


@app.on_message(filters.command(["owner",],prefixes=".") & filters.me)
def get_owner_stickers(_, message): 
    text = ""
    try:

        if t := get_user_data_text(message):
            name = message.reply_to_message.sticker.set_name
            text += f"https://t.me/addstickers/{name}\n"
            text += t
        else:
            set_names = get_emoji_set_names(message)
            for short_name in set_names:
                text += f"https://t.me/addemoji/{short_name} \n"
                text += get_user_data_text(short_name)
                text += "\n\n"
    except:  # noqa: E722
        text = "no sticker or custom emoji here"

    app.edit_message_text(message.chat.id, message.id, text, 
        parse_mode=ParseMode.MARKDOWN, 
        disable_web_page_preview=True)

app.run()
